package ipgather.core.ip;

import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.Consts;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.message.BasicHeader;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;

public class IpDownload {
	
	private static List<WebUrl> webUrls = new ArrayList<>();//要采集的普通地址

	static {
		String path = IpDownload.class.getResource("/").getPath() + "requests.properties";
		try {
			String uContent = FileUtils.readFileToString(new File(path), Consts.UTF_8);
			webUrls = JSON.parseObject(uContent,new TypeReference<List<WebUrl>>(){});
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		System.out.println("初始化读取url:"+JSON.toJSONString(webUrls));
	}
	
	
	private boolean hasNumberV(String host){
		if(StringUtils.isNotBlank(host)){
			return host.indexOf("${number}")!=-1;
		}
		return false;
	}
	
	private boolean hasPrefixV(String prefix){
		if(StringUtils.isNotBlank(prefix)){
			return prefix.indexOf("${prefix}")!=-1;
		}
		return false;
	}

	public static void main(String[] args) {
		IpDownload ipDownload = new IpDownload();
		
		List<HttpGet> requests = new ArrayList<>();
		HttpGet request = null;
		try {
			for(WebUrl webUrl : webUrls){
				String hString = null;
				if(ipDownload.hasNumberV(webUrl.getHost())){
					for(int i = webUrl.getMinPage();i<=webUrl.getMaxPage();i++){
						hString = webUrl.getHost();
						if(ipDownload.hasPrefixV(hString)){//判断是否有分页特殊字符串
							if(i>=Integer.parseInt(webUrl.getPrefixIndex())){//在第几个分页的时候加上
								hString = hString.replace("${prefix}", webUrl.getPrefix());
							}else{
								hString = hString.replace("${prefix}", "").replace("${number}", "");
							}
						}
						hString = hString.replace("${number}", i+"");
						request = new HttpGet(hString);
						requests.add(request);
					}
				}else{
					request = new HttpGet(new URI(webUrl.getHost()));
					requests.add(request);
				}
				if(null!=webUrl.getHeaders()){//请求头参数
					for(BasHeader basHeader : webUrl.getHeaders()){
						request.addHeader(new BasicHeader(basHeader.getName(), basHeader.getValue()));
					}
				}
			}
		} catch (URISyntaxException e) {
			e.printStackTrace();
		}
		
		PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
        cm.setMaxTotal(100);
        CloseableHttpClient httpclient = HttpClients.custom()
                .setConnectionManager(cm)
                .build();
        
        IpMatch match = new IpMatch(httpclient);
    	Set<String> ips = match.findIpsByRequest(requests);
    	System.out.println("筛选出IP地址数为："+ips.size());
    	match.validatorEnableIps(ips);
	}
}
